package mate.task.odin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
/**
 * The Registration view for a new user
 * @author Chris
 *
 */
public class RegisterActivity extends Activity{
	private EditText password, fullname, email, username;
	private Button register, cancel;
	
	/**
	 * The Create activity for Android
	 * Creates the initial interface
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		
		// Removes the top title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.register);
		
		password = (EditText) findViewById(R.id.passwordReg);
		fullname = (EditText) findViewById(R.id.fullnameRegister);
		email = (EditText) findViewById(R.id.emailRegister);
		username = (EditText) findViewById(R.id.usernameRegister);
		register = (Button) findViewById(R.id.regButton);
		cancel = (Button) findViewById(R.id.cancel);
		
		//Enter submits
        username.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        email.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});//Enter submits
        fullname.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
      //Enter submits
        password.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER){
					if (KeyEvent.ACTION_DOWN == event.getAction()){
						submit();
						return true;
					}
				}
				return false;
			}
		});
		
		
		
		
		//Fixes the password hint's font
        password.setTypeface(Typeface.DEFAULT);
        password.setTransformationMethod(new PasswordTransformationMethod());
	
        /*
        //Gives the password EditText a blue stroke
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				password.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg_stroke));
				username.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
				fullname.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
				email.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
			}
		});

        //Gives the email EditText a blue stroke
        email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				password.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
				username.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
				fullname.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
				email.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg_stroke));
			}
		});
        
        //Gives the username EditText a blue stroke
        username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
	
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			password.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
			username.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg_stroke));
			fullname.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
			email.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
		}
	});
        
        //Gives the fullname EditText a blue stroke
		fullname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
		
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			password.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
			username.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
			fullname.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg_stroke));
			email.setBackgroundDrawable(getResources().getDrawable(R.drawable.edittext_bg));
		}
	});
		*/
		register.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				submit();
			}
		});
		
		
		cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent revert = new Intent("mate.task.odin.TASKMATEACTIVITY");
				startActivity(revert);
			}
		});
	}
	
	public void submit(){
		UserDatabase db = UserDatabase.getDatabaseInstance();
		UserAccount temp = db.addUser(fullname.getText().toString(),email.getText().toString(),username.getText().toString(),password.getText().toString());
		//user is now logged in with new account, should go to todolist
		if(temp != null){
			Intent openToDo = new Intent("mate.task.odin.TUTORIALACTIVITY");
			startActivity(openToDo);
		}else{
			//print out error (bad login)
			AlertDialog.Builder alert = new AlertDialog.Builder(RegisterActivity.this);
			alert.setMessage("Username already in use.");
			alert.setTitle("Invalid Details");
			alert.setCancelable(true);
			alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {						
				@Override
				public void onClick(DialogInterface dialog, int which) {							
				}
			});
			alert.show();
		}				
	}
	
	/**
	 * Kills the activity on pause
	 */
	public void onPause() {
		super.onPause();
		finish();
	}
	
	
	
}
